<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 
 * 以权限的管理
 */


class RulesController extends CommonController {

		public function index()
		{	
			header("Content-type:text/html;charset=utf-8");

			$uids = M('auth_group')->select();

			foreach ($uids as $k => $v) {
				$uids[$k]['titles']=M('auth_rule')->where(array('id'=>array('in',$v['rules'])))->field('title')->select();
			}

			// var_dump($uids);

			$this->assign('rules',$uids);
			$this->display();


		}



	//添加动态的展示
		public function add()
		{

			$rules = M('auth_rule')->select();
			// var_dump($rules);
			$cate = array();
			foreach ($rules as $k=>$v) {
				// $cate[$k][] = $v['category'] ;
				$cate[$v['category']][] = $v;
			}
			// var_dump($cate);
			$this->assign('cate',$cate);
			$this->display();
		}

		//角色权限的添加
		public function add_rules()
		{
			// var_dump(I('post.'));
			
			$data = I('post.');

			$rules = rtrim($data['str'],',');
			// var_dump($data);
			$res=array();
			$res['title']=$data['data']['name'];
			$res['desc']=$data['data']['desc'];
			$res['rules']=$rules;
			// var_dump($res);
			if (M('auth_group')->add($res)) {
				$arr = array(
						'error'=>0,
						'info'=>'添加成功'
					);
				echo json_encode($arr);
			}

		}
		//角色权限的编辑
		public function edit()
		{
			//取所有数据
			$rules = M('auth_rule')->select();
			// var_dump($rules);
			$cate = array();
			foreach ($rules as $k=>$v) {
				// $cate[$k][] = $v['category'] ;
				$cate[$v['category']][] = $v;
			}
			// var_dump($cate);
			$this->assign('cate',$cate);
			//取原来选择的的数据
			$id=I('get.id');
			// var_dump($id);
			$this->assign('id',$id);
			$check = M('auth_group')->where(array('id'=>$id))->find();

			// $this->assign('check',$check);
			$arr = explode(',', $check['rules']);

			$check['rules']=$arr;
			// var_dump($arr);
			// var_dump($check);

			$this->assign('check',$check);
			
			$this->display();
			
			
		}

		//角色权限的更新编辑
		public function save_rules()
		{
			// var_dump(I('post.'));
			
			$data = I('post.');

			$id = current(I('post.'))['id'];
			// var_dump($id);
			$rules = rtrim($data['str'],',');
			// var_dump($data);
			$res=array();
			$res['title']=$data['data']['name'];
			$res['desc']=$data['data']['desc'];
			$res['rules']=$rules;
			
			if (M('auth_group')->where(array('id'=>$id))->save($res)) {
				$arr = array(
						'error'=>0,
						'info'=>'更新成功'
					);
				echo json_encode($arr);
			}

		}

		//角色权限的删除
		public function del()
		{
			$id = I('post.id');

			if (M('auth_group')->where(array('id'=>$id))->delete()) {
				$arr = array(
						'error'=>0,
						'info'=>'更新成功'
					);
				echo json_encode($arr);
			
			}
		}
		//批量删除
		public function delAll()
		{
			$id = rtrim(I('post.str'),',');
			$id = explode(',', $id);
			// var_dump($id);
			if (M('auth_group')->where(array('id'=>array('in',$id)))->delete()) {
				$arr = array(
						'error'=>0,
						'info'=>'删除成功'
					);
				echo json_encode($arr);
			
			}
		}
		//权限分类的展示
		public function cate()
		{	
			$cates = M('auth_rule')->field('id,category')->select();
			$this->assign('cates',$cates);
			$this->display();
		}
		//分类编辑展示
		public function cate_edit()
		{
			$id= I('get.id');
			// var_dump($id);
			$rows = M('auth_rule')->where(array('id'=>$id))->field('id,category')->find();
			$cates = M('auth_rule')->field('id,category')->select();
			$this->assign('rows',$rows);
			$this->assign('cates',$cates);
			$this->display();
		}
		//分类修该更新分类
		public function changeEdit()
		{
			// var_dump(I('post.'));

			$data['id']= I('post.id');
			$data['category']=I('post.cname');
			// var_dump($data);exit();
			if (M('auth_rule')->save($data)) {
				$arr = array(
						'error'=>0,
						'info'=>'更新成功'
					);
				echo json_encode($arr);
			}
		}
		//规则表展示
		public function rule()
		{
			$count =  M('auth_rule')->count();
			 // var_dump($count);
			 $this->assign('count',$count);
			$Page = new \Think\Page($count,3);
			$show = $Page->show();
			$this->assign('page',$show);
			$rules = M('auth_rule')->limit($Page->firstRow,$Page->listRows)->select();
			// var_dump($rules);
			$this->assign('rules',$rules);
			$this->display();
		}
		//编辑页面的展示
		public function rule_edit()
		{
			$id = I('get.id');
			// var_dump($id);

			$res =M('auth_rule')->where(array('id'=>$id))->find();
			// var_dump($res);
			$this->assign('res',$res);
			$this->display();

		}

		public function changeRule()
		{
			$data = I('post.');
			
			
			if (M('auth_rule')->save($data)) {
				
				$arr = array(
						'error'=>0,
						'info'=>'更新成功'
					);
				echo json_encode($arr);
			}
		}
		//一次删除多个规则
		public function rules_delAll()
		{
			// var_dump(I('post.'));
			$id = rtrim(I('post.str'),',');
			$id = explode(',', $id);
			
			if (M('auth_rule')->where(array('id'=>array('in',$id)))->delete()) {
				$arr = array(
						'error'=>0,
						'info'=>'删除成功'
					);
				echo json_encode($arr);
			
			}
		}
				//规则权限的删除
		public function rules_del()
		{
			$id = I('post.id');

			if (M('auth_rule')->where(array('id'=>$id))->delete()) {
				$arr = array(
						'error'=>0,
						'info'=>'删除成功'
					);
				echo json_encode($arr);
			
			}
		}
		//添加规则
		public function rules_add()
		{
				$data = I('post.');
				// var_dump($data);
				$res['category'] = trim($data['category']);
				$res['title'] = trim($data['name']);
				$res['name'] =trim($data['rules']);
			if ($id = M('auth_rule')->add($res)) {
				$arr = array(
						'error'=>0,
						'info'=>'添加成功',
						'id'=>$id
					);
				// var_dump($arr);
				echo json_encode($arr);
			}else{
				$arr = array(
						'error'=>1,
						'info'=>'添加失败',
						
					);
				// var_dump($arr);
				echo json_encode($arr);
			}
		}

}